var express = require('express');
var router = express.Router();
var https = require('https');

var MongoClient = require('mongodb').MongoClient;
var DB_CONN_STR = 'mongodb://127.0.0.1:27017/project';

router.all('*', function(req, res, next) {
    res.header("Access-Control-Allow-Origin", "*");
    res.header('Access-Control-Allow-Methods', 'PUT, GET, POST, DELETE, OPTIONS');
    res.header("Access-Control-Allow-Headers", "X-Requested-With");
    res.header('Access-Control-Allow-Headers', 'Content-Type');
    next();
});
/* GET home page. */
router.get('/goodsdetails', function(req, res) {
    var page = req.query.page;
    var id = req.query.id;
    console.log(id);
    https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=22.583312&longitude=113.9568599&keyword=&offset=' + page + '&limit=20&extras[]=activities&restaurant_category_ids[]=' + id, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
});
router.get('/goodsdetails/left', function(req, res) {
    https.get('https://mainsite-restapi.ele.me/shopping/v2/restaurant/category?latitude=22.583312&longitude=113.9568599', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 首页请求数据
router.get('/banner', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://mainsite-restapi.ele.me/shopping/v2/entries?latitude=22.58067&longitude=113.96205&templates[]=main_template', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})
router.get('/shop', function(req, res) {
	 var id = req.query.id;
    https.get('https://mainsite-restapi.ele.me/shopping/v2/menu?restaurant_id='+id, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})


// 首页搜索列表数据
router.get('/suggest', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://restapi.ele.me/shopping/v3/hot_search_words?latitude=22.54286&longitude=114.059563', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

router.get('/shop/head', function(req, res) {
	 var id = req.query.id;
    https.get('https://mainsite-restapi.ele.me/shopping/restaurant/'+id+'?extras[]=activities&extras[]=albums&extras[]=license&extras[]=identification&latitude=22.53419&longitude=113.94517', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 首页天气数据
router.get('/weather', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://mainsite-restapi.ele.me/bgs/weather/current?latitude=22.58067&longitude=113.96205', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 首页商品列表数据
router.get('/indexList', function(req, res) {
        var time = new Date().getTime();
        var page = req.query.page;
        console.log(page)
        https.get('https://mainsite-restapi.ele.me/shopping/restaurants?latitude=22.54286&longitude=114.059563&offset=' + page + '&limit=20&extras[]=activities&terminal=h5', function(response) {
            var data = '';
            response.on('data', function(chunk) {
                data += chunk;
            })
            response.on('end', function() {
                res.send(data);
            })
        })
    })
    // 发现页活动商城数据
router.get('/activity', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://mainsite-restapi.ele.me/member/v1/discover?platform=1&block_index=0', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

router.get('/shop/comment', function(req, res) {
	 var id = req.query.id;
	 var offset=req.query.offset;
	 var tag_name=encodeURIComponent(req.query.tag_name);
    https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+id+'/ratings?has_content=true&offset='+offset+'&limit=10&tag_name='+tag_name, function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

router.get('/shop/rating', function(req, res) {
	 var id = req.query.id;
    https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+ id +'/ratings/scores', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

router.get('/shop/tag', function(req, res) {
	 var id = req.query.id;
    https.get('https://mainsite-restapi.ele.me/ugc/v2/restaurants/'+ id +'/ratings/tags', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 发现页美食热推数据
router.get('/hot', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://mainsite-restapi.ele.me/hotfood/v1/guess/likes?latitude=22.5996558&longitude=114.0487129&offset=0&limit=3&request_id=6e912408-f242-4526-b4ac-a5adbb34371e&tag_id=-1&columns=1', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 发现页积分商城数据
router.get('/limit', function(req, res) {
    var time = new Date().getTime();
    var num = req.query.id;
    https.get('https://mainsite-restapi.ele.me/member/gifts/suggest', function(response) {
        var data = '';
        response.on('data', function(chunk) {
            data += chunk;
        })
        response.on('end', function() {
            res.send(data);
        })
    })
})

// 登录
router.get('/reg', function(req, res, next) {
    // 先要获取前端传过来的数据
    var name = req.query.name;
    var pwd = req.query.pwd;

    MongoClient.connect(DB_CONN_STR, function(err, db) {
        if (err) {
            console.log('链接数据库失败' + err);
            res.send({ code: -1, msg: '网络异常，请稍候重试' });
        } else {

            // 操作那个 users 集合
            var conn = db.collection('users');
            conn.find({ name: name }).count(function(err, num) {
                if (!num) {
                    conn.insertOne({ name: name, pwd: pwd }, function(err, info) {
                        if (err) {
                            // 错误日志
                            console.log('注册失败' + err);
                            res.send({ code: -2, msg: '注册失败' });
                        } else {
                            res.send({ code: 0, msg: '注册成功！！！' });
                        }

                    })
                } else {
                    conn.find({ name: name, pwd: pwd }).count(function(err, num) {
                        if (!num) {
                            res.send({ code: -2, msg: '账号或密码错误' });
                        } else {
                            res.send({ code: 0, msg: '登录成功!' });

                            db.close();
                        }
                    })
                }
            })
        }
    })
});
module.exports = router;